/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.wii.elpro.web.action;

import com.wii.elpro.dao.hb.PriceDAOImpl;
import com.wii.elpro.entity.Price;
import com.wii.elpro.entity.User;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

/**
 *
 * @author Vyor
 */
public class ProductPreSaveAction extends Action {

    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) throws Exception {
        HttpSession sess = request.getSession(true);
        User user = (User)sess.getAttribute("userSession");       
        if(user==null){
            return mapping.findForward("gagalLogin");            
        }  
        String forward;
        ActionMessages messages = null;
        
        try {
            List<Price> p = new PriceDAOImpl().getPrices();
            if(p.isEmpty()) {
                messages = new ActionMessages();
                messages.add("message", new ActionMessage("customMessage", "This form need Price (Type/Unit) data but it is not found in data source. <br/>" +
                        "Please insert it first and try again. " +
                        "Klik <a href=\"prc_presave.do\">here</a> to add Price."));
                saveMessages(sess, messages);
            }
            request.setAttribute("prices", p);            
            forward = "success";
        } catch (Exception ex) {
            forward = "fail";
            ex.printStackTrace();
        }
        return mapping.findForward(forward);
    }
}
